﻿
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_Rights_CanAccessByPerson_V2]') AND type in (N'P', N'PC'))
    DROP PROCEDURE [dbo].[sproc_Rights_CanAccessByPerson_V2];
GO
CREATE PROCEDURE [dbo].[sproc_Rights_CanAccessByPerson_V2] 
    @ClassID    int,
    @StaffID    int,
    @Act_ID     int,
    @AllowAccess    bit output
/*

======================================================================
功能:    得到个人@UserName是否能对@ClassID组进行@Act_ID操作
参数:
    @ClassID int    :    组（类）ID
    @StaffID int    :    用户编号
    @Act_ID int     :    权利代号

备注:    继承指是否继承在父组中的权利(在父组中有权利即在此组中有权利)
======================================================================

*/
AS
BEGIN
SET NOCOUNT ON

SET @AllowAccess = 0;

--得到个人@UserName是否能对组@ClassID进行@Act_ID操作
If EXISTS(SELECT 1 
    FROM
        UDS_Assign_Rule
    WHERE     
        Staff_ID = @StaffID
        and Act_ID = @act_id 
        and Based_On = 4 /* staff */ 
        and Team_ID = 0 /* 0 indicates global class */
    ) 
BEGIN
    SET @AllowAccess = 1;
    RETURN;
END

--是否可以访问指定类的父类？
IF EXISTS(
    SELECT 1 
        FROM 
            dbo.UDS_Assign_Rule
        WHERE
            Staff_ID = @StaffID
            AND Act_id = @act_id
            AND based_on = 4 /* staff */
            AND Team_ID IN (
                SELECT ClassID
                FROM dbo.fn_get_parent_class(@ClassID)
            )
)
BEGIN
    SET @AllowAccess = 1;
END

END
GO